//
//  LoginViewController.m
//  SSIL
//
//  Created by 余胜民 on 16/5/12.
//  Copyright © 2016年 sanmy. All rights reserved.
//

#import "LoginViewController.h"
#import <AFNetworking.h>

static NSString * const kLoginSuccessString = @"1";
static NSString * const kMainTabStoryboardID = @"MainTabControllerID";

@interface LoginViewController ()

@property (weak, nonatomic) IBOutlet UITextField *accountTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@end

@implementation LoginViewController

#pragma LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
    
}


- (void)goBack {
    if (_isPresent) {
        [self.view endEditing:YES];
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }else{
        [super goBack];
    }
}

#pragma IB Action
- (IBAction)loginAction:(UIButton *)sender {
 
    if ([self checkAccountAndPassWord]) {
        [self.view endEditing:YES];
        [self showLoadingView];
        [self userLoginWithAccount:_accountTextField.text andPassword:_passwordTextField.text];
    }
    
}

- (BOOL)checkAccountAndPassWord {
    NSString * account = [_accountTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    NSString * password = [_passwordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    
    UIButton * button = (UIButton *)[self.view viewWithTag:101];
    if (account.length < 1 || password.length < 1 ) {
        button.enabled = NO;
        return NO;
    }
    button.enabled = YES;
    return YES;
}

- (void)userLoginWithAccount:(NSString *)account andPassword:(NSString *)password {
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    session.responseSerializer = [AFHTTPResponseSerializer serializer];
    [session POST:@"http://123.56.242.204/StdInterface/Ajaxdata/StdApp.ashx" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSMutableData *operTypeData = [[NSMutableData alloc] init];
        NSMutableData *accountData = [[NSMutableData alloc] init];
        NSMutableData *passwordData = [[NSMutableData alloc] init];
        [operTypeData appendData:[[NSString stringWithFormat:@"1"] dataUsingEncoding:NSUTF8StringEncoding]];
        [accountData appendData:[account dataUsingEncoding:NSUTF8StringEncoding]];
        [passwordData appendData:[password dataUsingEncoding:NSUTF8StringEncoding]];
        [formData appendPartWithFormData:operTypeData name:@"OperType"];
        [formData appendPartWithFormData:accountData name:@"memberid"];
        [formData appendPartWithFormData:passwordData name:@"password"];
        
    } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self dismissLoadingView];
        NSLog(@"%@",[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil]);
        
        NSDictionary *resultDict =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        if ([[resultDict objectForKey:@"resultdata"] isEqualToString:kLoginSuccessString]) {
            
            UITabBarController *tabbarController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:kMainTabStoryboardID];
            [UIApplication sharedApplication].keyWindow.rootViewController = tabbarController;
        }else {
            //TODO: 密码或帐号不对，弹出提示
        }
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

@end
